/******************************************************************************
*
* This file is part of FreeTrack (http://code.google.com/p/qtfreetrack).
* BY Willy Gardiol (willy@gardiol.org)
*
* FreeTrack is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FreeTrack is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar.  If not, see <http://www.gnu.org/licenses/>
*
******************************************************************************/

#include "rotatingdialog.h"
#include "rotatingwidget.h"
#include "ftcontrols.h"

RotatingDialog::RotatingDialog() :
    QWidget( 0, Qt::Window )
{
#ifdef Q_WS_MAEMO_5
    setAttribute(Qt::WA_Maemo5AutoOrientation, true);
#else
    setAttribute( Qt::WA_AutoOrientation, true );
#endif
    setBackgroundRole( QPalette::Window );
    setContextMenuPolicy(Qt::NoContextMenu);
    connect( qApp->desktop(), SIGNAL(workAreaResized(int)),
             this, SLOT(screenResized(int)) );
}

int RotatingDialog::exec()
{
    showFullScreen();
    int ret = loop.exec();
    hide();
    return ret;
}

void RotatingDialog::exitLoop(int x)
{
    loop.exit(x);
}

void RotatingDialog::accept()
{
    exitLoop(QDialog::Accepted);
}

void RotatingDialog::reject()
{
    exitLoop(QDialog::Rejected);
}

void RotatingDialog::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.fillRect( rect(), QApplication::palette().brush( FT_COLOR_GROUP, QPalette::Window ) );
}

void RotatingDialog::screenResized(int s)
{
    // This incredibly fucking awful fix is due to some obscure internal Symbian BUG
    if ( rect().size() != qApp->desktop()->screenGeometry(s).size() )
    {
        bool hack = isVisible();
        if ( hack )
            showNormal();
        setGeometry(qApp->desktop()->screenGeometry(s));
        if ( hack )
            showFullScreen();
    }
    rotateDialog();
}

void RotatingDialog::resizeEvent(QResizeEvent* ev)
{
    QWidget::resizeEvent( ev );
    if ( layout() == NULL )
        rotateDialog();
}

void RotatingDialog::rotateDialog()
{
    FTControls::Orientation o = width() > height() ?
                                FTControls::horizontal :
                                FTControls::vertical;

    QLayout* new_layout = NULL;
    switch ( o )
    {
    case FTControls::vertical:
        new_layout =  createVertical();
        break;
    case FTControls::horizontal:
        new_layout = createHorizontal();
        break;
    default:
        new_layout = NULL;
    }
    // If we have a new layout...
    if ( new_layout != NULL )
    {   // Delete old layout first (required by Qt)
        if ( layout() != NULL )
            delete layout();
        // then set the new layout.
        new_layout->setSpacing(0);
        new_layout->setContentsMargins( 0,0,0,0 );
        new_layout->setSizeConstraint( QLayout::SetMinAndMaxSize );
        setLayout( new_layout );
        new_layout->invalidate();
        new_layout->activate();
        new_layout->update();
    }
}

QLayout* RotatingDialog::createHorizontal()
{
    return createVertical();
}

QLayout* RotatingDialog::createVertical()
{
    return NULL;
}
